<?php
/**
 * Created by PhpStorm.
 * User: yuxingkui
 * Date: 17/8/11
 * Time: 下午1:16
 */
namespace App\Api\Controllers\Validators;

use Illuminate\Http\Request;
use Validator;

class CommentValidator
{

    /**
     * @param Request $request
     * @return array
     * @author Liying
     * @name 发布评论
     */
    public static function commentCreate(Request $request)
    {
        $only = [
            'content',
            'comment_id',
            'sender_author',
            'receiver_author',
            'object_id',
            'object_child_id',
            'comment_type',
            'weight',
            'tag_ids',
            'star_num',
            'created_at',
        ];

        $rules = [
            'object_id' => 'required',
            'star_num' => 'required',

        ];

        $messages = [
            'object_id.required' => '无法定位到发布对象',
            'star_num.required' => '星级数量',
        ];

        $input = $request->only($only);

        $validator = Validator::make($input, $rules, $messages);

        if ($validator->fails())
            exit(json_encode(['info' => $validator->errors()->first(), 'code' => '1002']));

        return $input;
    }

    /**
     * @param Request $request
     * @return array
     * @name 评论添加新规则
     * @user IHammer
     */
    public static function commentCreateRule(Request $request)
    {
        $only = [
            'content',
            'comment_id',//回复的时候使用
            'sender_author',
            'receiver_author',//接收评论人
            'object_id',
            'object_child_id',
            'comment_type',
        ];

        $rules = [
            'content' => 'required|max:140',
            'object_id' => 'required',
            'sender_author' => 'required',

        ];

        $messages = [
            'object_id.required' => '无法定位到发布对象',
            'content.required' => '评论内容不能为空',
            'content.max' => '评论内容不能超过140个字',
            'sender_author.required' => '登录信息错误',
        ];

        $input = $request->only($only);

        $validator = Validator::make($input, $rules, $messages);

        if ($validator->fails())
            exit(json_encode(['info' => $validator->errors()->first(), 'code' => '1002']));

        return $input;
    }

}